package com.toobulkeh.compuprov.prop;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.apache.commons.io.FileUtils;

public class PropPicker extends JFrame {

	private static final long serialVersionUID = 3700173863366846957L;
	private static final String INPUT_OBJ_DIR = "C:\\input\\objs";
	private static final String OUTPUT_OBJ_DIR = "C:\\output\\objs";
	private Container cp;
	private ImageListener imgHandler;
	private static final int WINDOW_WIDTH = 1024;
	private static final int WINDOW_HEIGHT = 768;
	private static final Dimension WINDOW_SIZE = new Dimension(WINDOW_WIDTH,WINDOW_HEIGHT);
	private static final Dimension IMG_THUMB_SIZE = new Dimension(150,150);
	private JLabel chosenProp;
	
	public PropPicker() {
		this.setPreferredSize(WINDOW_SIZE);
		imgHandler = new ImageListener();
		cp = this.getContentPane();
		mainPanel();
	}
	
	public void thanksDialog(JLabel imgThumb) {
		mainPanel();
		JOptionPane.showMessageDialog(this, "Nice Prop!", "Prop Chosen", JOptionPane.PLAIN_MESSAGE, chosenProp.getIcon());
	}
	
	public void mainPanel() {
		cp.removeAll();
		JPanel imgP = new JPanel();
		JPanel imgsP = new JPanel(new GridLayout(0,6));
		
		ImageIcon tmpIcon;
		BufferedImage tmpImg;
		JLabel tmpLabel;
		File list[] = (new File(INPUT_OBJ_DIR)).listFiles();
		File tmp;
		double ratio;
		int thumbHeight, thumbWidth;
		
		//System.out.println("Object Files Found: "+list.length);
		
		for(int i = 0; i < list.length; i++) {
			try {
				tmp = list[i];
				tmpImg = ImageIO.read(tmp);
				//System.out.println("File: "+tmp.getAbsolutePath());
				//System.out.println("Original Image Dimensions: ("+tmpImg.getWidth()+","+tmpImg.getHeight()+").");
				if((tmpImg.getHeight() > IMG_THUMB_SIZE.height) ||
				   (tmpImg.getWidth() > IMG_THUMB_SIZE.width)) {
					//System.out.println("Resizing image...");
					ratio = (double)tmpImg.getWidth()/(double)tmpImg.getHeight();
					//System.out.println("Calculated Ratio: "+ratio);
					if(ratio < 1) {
						thumbHeight = IMG_THUMB_SIZE.height;
						thumbWidth = (int)(IMG_THUMB_SIZE.width * ratio);
					} else {
						thumbWidth = IMG_THUMB_SIZE.width;
						thumbHeight = (int)(IMG_THUMB_SIZE.height / ratio);
					}
					//System.out.println("Thumb Image Dimensions: ("+thumbWidth+","+thumbHeight+").");
					tmpIcon = new ImageIcon(tmpImg.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH));
					//System.out.println("Thumb Ratio"+tmpIcon.getIconWidth()/tmpIcon.getIconHeight());
				} else { //don't resize
					tmpIcon = new ImageIcon(tmpImg);
				}
				//System.out.println("-----------------");
				
				tmpLabel = new JLabel(tmpIcon);
				tmpLabel.setMaximumSize(IMG_THUMB_SIZE);
				tmpLabel.setPreferredSize(IMG_THUMB_SIZE);
				tmpLabel.setBorder(BorderFactory.createLineBorder(Color.black));
				tmpLabel.setName("obj"+i);
				tmpLabel.addMouseListener(imgHandler);
				imgsP.add(tmpLabel);
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, "Error Creating Image", "Error!", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
			}

		}
		
		JScrollPane imgsSP = new JScrollPane(imgsP);
		imgP.setPreferredSize(WINDOW_SIZE);
		cp.add(imgsSP);
		this.pack();
	}
	
	public void saveImg(JLabel source) {
		thanksDialog(source);
		File list[] = (new File(INPUT_OBJ_DIR)).listFiles();
		File tmp;
		int i = Integer.parseInt(source.getName().substring(3));
		tmp = list[i];
		
		File out = new File(OUTPUT_OBJ_DIR + "\\" + tmp.getName());
		try {
			FileUtils.copyFile(tmp, out);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(this, "Error Saving Image", "Error!", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
		System.out.println("Saved obj file: "+tmp.getName());
	}
	
	class ImageListener implements MouseListener {
		public void mouseClicked(MouseEvent e) {
			chosenProp = (JLabel)e.getSource();
			System.out.println("You clicked on img: "+chosenProp.getName());
			saveImg(chosenProp);
		}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
	}
	
	public static void main(String[] args) {
	      PropPicker mainframe = new PropPicker();
	      //mainframe.pack();
	      mainframe.setTitle("Compuprov Prop Picker!");
	      mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	      mainframe.setVisible(true);
	}
}
